package com.servo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.servo.mapper.UnitMapper;
import com.servo.model.UnitDO;
import com.servo.service.UnitService;
import com.servo.utils.JsonData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 单位表 服务实现类
 * </p>
 *
 * @author fwj
 * @since 2022-01-07
 */
@Service
public class UnitServiceImpl implements UnitService {

    @Resource
    private UnitMapper unitMapper;

    @Override
    public JsonData save(UnitDO unitDO) {
        unitMapper.insert(unitDO);
        return JsonData.buildSuccess();
    }

    @Override
    public JsonData list() {
        List<UnitDO> list = unitMapper.selectList(new QueryWrapper<UnitDO>().orderByAsc("sort"));
        return JsonData.buildSuccess(list);
    }

    @Override
    public JsonData pageList(Long page, Long limit) {
        IPage<UnitDO> doPage = new Page<>();
        doPage.setPages(page);
        doPage.setSize(limit);
        IPage<UnitDO> selectPage = unitMapper.selectPage(doPage, new QueryWrapper<UnitDO>().orderByAsc("sort"));
        return JsonData.buildSuccess(selectPage);
    }

    @Override
    public JsonData updateIsUse(Long id) {
        UnitDO unitDO = new UnitDO();
        unitDO.setId(id);
        unitDO.setIsUse(1);
        unitMapper.updateById(unitDO);
        return JsonData.buildSuccess();
    }

    @Override
    public JsonData remove(Long id) {
        unitMapper.deleteById(id);
        return JsonData.buildSuccess();
    }
}
